<template>
  <el-container class="layout-container">
    <el-header class="header">
      <div class="logo">
        <img src="https://www.flyingwavestravel.com/themes/template/assets/img/fw.png" alt="Logo" />
        <span>Demo 示例</span>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <el-button type="primary" size="small">
            系统设置<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout" divided>登出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container class="main-container">
      <el-aside width="220px" class="aside">
        <el-scrollbar>
          <el-menu
              :default-active="activeIndex"
              class="el-menu-vertical"
              @select="handleSelect"
              :collapse="isCollapse"
          >
            <el-sub-menu index="1">
              <template #title>
                <el-icon><Document /></el-icon>
                <span>订单管理</span>
              </template>
              <el-menu-item index="/preorder">新建预下单</el-menu-item>
              <el-menu-item index="/pre-order-details">加载预下单</el-menu-item>
              <el-menu-item index="/create-order">创建订单</el-menu-item>
              <el-menu-item index="/order-details">订单详情</el-menu-item>
              <el-menu-item index="/order-list">订单列表</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon><wallet /></el-icon>
                <span>支付管理</span>
              </template>
              <el-menu-item index="/paypal-payment">发起支付</el-menu-item>
              <el-menu-item index="/payment-status">支付状态</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-scrollbar>
      </el-aside>

      <el-main class="main">
        <el-breadcrumb :separator-icon="ArrowRight" class="breadcrumb">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ currentRoute }}</el-breadcrumb-item>
        </el-breadcrumb>
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  Document,
  Wallet,
  ArrowRight,
  ArrowDown,
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const isCollapse = ref(false)
const activeIndex = computed(() => route.path)
const currentRoute = computed(() => route.name)

const handleSelect = (index) => {
  router.push(index)
}

const handleCommand = (command) => {
  if (command === 'logout') {
    logout()
  } else if (command === 'settings') {
    router.push('/settings')
  }
}

const logout = () => {
  localStorage.removeItem('authToken')
  router.push('/login')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
  z-index: 1;
}

.logo {
  display: flex;
  align-items: center;
}

.logo img {
  height: 32px;
  margin-right: 10px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.main-container {
  height: calc(100vh - 60px);
}

.aside {
  background-color: #f5f7fa;
  border-right: 1px solid #e6e6e6;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 220px;
}

.main {
  background-color: #f0f2f5;
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
